# -*- coding: utf-8 -*
import random
import time
import datetime
from TimeTool import *


class Output():
    titles = ['姓名', '日期时间', '签到方式', '说明']
    type = ["打卡", "指纹"]

    morning = ["7:40:00", "9:10:00"]
    afternoon = ["17:50:00", "20:10:00"]

    overtime_morning = ["9:00:00", "10:30:00"]
    overtime_afternoon = ["17:00:00", "18:30:00"]

    leave_percent = 2

    def __init__(self, ws, peoples, year, month, exclude, overtime):
        self.ws = ws
        self.x = 0
        self.peoples = peoples
        self.month = month
        self.year = year
        self.exclude = exclude
        self.pday = 0
        self.overtime = overtime

    def write_title(self):
        for i in range(len(Output.titles)):
            self.ws.write(self.x, i, Output.titles[i])
        self.x += 1

    def gen(self):
        self.write_title()
        self.gen_time()
        return [str(self.year) + "-" + str(self.month) + "  person of day : ", str(self.pday)]

    def gen_time(self):
        for name in self.peoples:
            for day in range(1, 32):
                if not is_valid_date(self.year, self.month, day):
                    break
                if self.is_work_day(day):
                    self.work_gen(day, name)
                else:
                    self.overtime_gen(day, name)

    def work_gen(self, day, name):
        leave = random.randint(0, 100)
        if leave < Output.leave_percent:
            self.ws.write(self.x, 0, name)
            self.ws.write(self.x, 1, datetime.datetime(self.year, self.month, day).strftime("%Y-%m-%d"))
            self.ws.write(self.x, 3, '请假')
            self.x += 1
            return
        self.write_data(day, name, Output.morning)
        self.write_data(day, name, Output.afternoon)
        self.pday += 1

    def overtime_gen(self, day, name):
        overtime = random.randint(0, 100)
        if overtime < self.overtime:
            self.ws.write(self.x, 3, '加班')
            self.write_data(day, name, Output.overtime_morning)
            self.ws.write(self.x, 3, '加班')
            self.write_data(day, name, Output.overtime_afternoon)
            self.pday += 1

    def write_data(self, day, name, time):
        self.ws.write(self.x, 0, name)
        self.ws.write(self.x, 1, self.random_time(day, time[0], time[1]))
        self.ws.write(self.x, 2, self.random_type())
        self.x += 1

    def random_type(self):
        type = random.randint(0, 10)
        if type < 7:
            return Output.type[0]
        else:
            return Output.type[1]

    def is_work_day(self, day):

        for exDay in self.exclude:
            if is_after(exDay, datetime.datetime(self.year, self.month, day)):
                break
            if is_equals(exDay, datetime.datetime(self.year, self.month, day)):
                return False

        return not is_week_end(self.year, self.month, day)

    def random_time(self, day, start, end):
        D = str(self.year) + "-" + str(self.month) + "-" + str(day) + " "
        return random_time(D + start, D + end)
